home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
DB_CLIPP
/
0411.ZIP
/
ENCRYPT.C
< prev
next >
Wrap
Text File
|
1985-09-18
|
4KB
|
133 lines
#include "stdio.h"
main(argc,argv)
int argc;
char *argv[];
{
int c,
i,
infile,
outfile,
passlen;
char password[9],
passchek[9];
if (argc == 1)
{
exit(0);
}
if (argv[1][0] == '#')
{
puts("\n");
puts("Program: ENCRYPT\n");
puts("Author : Peter Townsend\n");
puts("Date : 18Sep85\n");
puts("Time : 12:27\n");
puts("Version: 1.1\n");
exit(0);
}
if (argv[1][0] == '?')
{
puts("\n");
puts("This program reads a file and, character by character, adds a\n");
puts("key word (modulo 256) to the character.\n");
puts("\n");
puts("There are 3 parameters on the command line:\n");
puts(" 1. Input filename - the name of the file which is to be\n");
puts(" (de-)encrypted.\n");
puts(" 2. Output filename - the name of the file into which the\n");
puts(" (de-)encrypted data is to be placed.\n");
puts(" 3. Encrypt switch - literal, either /ON or /OFF indicating\n");
puts(" whether, respectively, the data is to\n");
puts(" be encrypted or de-encrypted.\n");
puts("\n");
puts("Syntax: ENCRYPT [input filename] [output filename] /on|/off \n");
puts("\n");
puts("Example: ENCRYPT coded.dbf myfile.dbf /on\n");
puts(" will ask for a password which it will use to create\n");
puts(" the coded file coded.dbf from myfile.dbf.\n");
puts("Example: ENCRYPT coded.dbf myfile.dbf /off\n");
puts(" will ask for a password which it will use to create\n");
puts(" an uncoded file myfile.dbf from coded.dbf.\n");
puts("\n");
puts("NB If more than 1 person encrypts a file, each using their\n");
puts(" own password, then the result is not legible to either with-\n");
puts(" out the other applying their password.");
exit(0);
}
if (argc == 2)
{
printf("\nMissing Output Filename\n");
exit(1);
}
if (argc == 3)
{
printf("\nMissing Encrypt switch\n");
exit(1);
}
if (strcmp(argv[1],argv[2]) == 0)
{
printf("\nInput filename and output filename cannot be the same.\n");
exit(1);
}
for(i=0;i<strlen(argv[3]);i++)
{
argv[3][i] = toupper(argv[3][i]);
}
if (strcmp(argv[3],"/ON") != 0)
{
if (strcmp(argv[3],"/OFF") != 0)
{
printf("\nInvalid encrypt switch ---> %s\n",argv[3]);
exit(1);
}
}
if ((infile = open(argv[1],0)) == -1)
{
printf("\nCannot open input file ---> %s\n",argv[1]);
exit(1);
}
if ((outfile = open(argv[2],1)) == -1)
{
if ((outfile = creat(argv[2])) == -1)
{
printf("\nCannot open/create output file ---> %s\n",argv[2]);
exit(1);
}
}
printf("\nPlease enter your password ===> ");
scanf("%8s",password);
for(i=0;i<24;i++)
{
puts("\n");
}
printf("\nPlease re-enter your password ===> ");
scanf("%8s",passchek);
for(i=0;i<24;i++)
{
puts("\n");
}
if ((i = strcmp(passchek,password)) != 0)
{
printf("\nPasswords do not match ... ENCRYPT terminating.\n");
exit(1);
}
passlen = strlen(password);
if (strcmp(argv[3],"/OFF") == 0)
{
for(i=0;i<passlen;i++)
{
password[i] = 256 - password[i];
}
}
i = passlen;
printf("\nEncrypting ... Do NOT disturb.\n");
while ((c = fgetc(infile)) != EOF)
{
i = (++i) % passlen;
c = (c + password[i]) % 256;
putc(c,outfile);
}
printf("\nEncryption finished.\n");
fclose(infile);
fclose(outfile);
}